﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace WebCache
{
    public class CacheProviders
    {
        private const string SESSION_CACHE_OBJECT_KEY = "_SESSION_CACHE_";
        private const string GLOBAL_CACHE_OBJECT_KEY = "_GLOBAL_CACHE_";
        private static SessionCache clientInstance = null;
        
        public static SessionCache SessionCache
        {
            get
            {
                SessionCache instance = null;
                if (System.Web.HttpContext.Current != null)
                {
                    instance = HttpContext.Current.Session[SESSION_CACHE_OBJECT_KEY] as SessionCache;
                    if (instance == null) HttpContext.Current.Session[SESSION_CACHE_OBJECT_KEY] = (instance = new SessionCache());
                    return instance;
                }
                else
                {
                    if (clientInstance == null) clientInstance = new SessionCache();
                    instance = clientInstance;
                }
                return instance;
            }
        }

        private static GlobalCache globalCache = null;
        public static GlobalCache GlobalCache
        {
            get
            {
                GlobalCache instance = null;
                if (System.Web.HttpContext.Current != null)
                {
                    instance = HttpContext.Current.Cache[GLOBAL_CACHE_OBJECT_KEY] as GlobalCache;
                    if (instance == null) HttpContext.Current.Cache[SESSION_CACHE_OBJECT_KEY] = (instance = new GlobalCache());
                    return instance;
                }
                else
                {
                    if (globalCache == null) globalCache = new GlobalCache();
                    instance = globalCache;
                }
                return instance;
            }
        }
    }
}
